package com.yswwpp.cross;

import java.nio.charset.CharacterCodingException;

import org.apache.mina.core.buffer.IoBuffer;

public abstract class Encoder extends Coder
{
	public void putString(IoBuffer buffer, String str)
			throws CharacterCodingException
	{
		int strLen = safeGetByteLength(str);
		buffer.putInt(strLen);
		if (strLen > 0)
		{
			buffer.putString(str, ENCODER_CHARSET);
		}
	}

	public void putBoolean(IoBuffer buffer, boolean value)
	{
		buffer.put(value ? (byte) 1 : (byte) 0);
	}

	public void putByteArray(IoBuffer buffer, byte[] content)
			throws CharacterCodingException
	{
		if (content == null)
		{
			buffer.putInt(0);
		} else
		{
			buffer.putInt(content.length);
			buffer.put(content);
		}

	}
}
